/**
 * 
 */
package ch.unizh.ifi.ailab.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

import ch.unizh.ifi.ailab.camera.Camera;
import ch.unizh.ifi.ailab.config.LocalizationSettings;
import ch.unizh.ifi.ailab.config.ProgramSettings;
import ch.unizh.ifi.ailab.suitcase.Suitcase;
import ch.unizh.ifi.ailab.tests.MockSuitcase;

/**
 * @author hans jaeckle jaeckle@gmail.com
 * 
 */
public class SteeringTab extends JPanel {

	private static final long serialVersionUID = 1L;

	private JPanel remotePanel = null;

	private JButton fasterButton = null;

	private JButton remoteButton = null;

	private JButton leftButton = null;

	private JButton rightButton = null;

	private JButton slowerButton = null;

	private JButton stopButton = null;

	private JButton suitcaseButton = null;

	private JPanel steeringInfoPanel = null;

	// imagePanel = 320x240
	private JPanel imagePanel = null;

	private JPanel cameraPanel = null;

	private JButton cameraButton = null;

	private JLabel jLabel1 = null;

	private JLabel jLabel2 = null;

	private JLabel jLabel3 = null;

	private JLabel jLabel4 = null;

	private JButton recordButton = null;

	private JButton snapButton = null;

	private JLabel infoLine2 = null;

	private JLabel infoLine1 = null;

	private JLabel infoLine3 = null;

	private JLabel infoLine4 = null;

	private JLabel jLabel5 = null;

	// die lokalisierung
	private static LocalizationSettings localization = LocalizationSettings
			.getLocalizationSettings();

	// die properties
	private static ProgramSettings properties = null;

	// der koffer
	private Suitcase suitcase = null; // @jve:decl-index=0:

	// die kamera
	private Camera camera = null;

	// logging
	private Logger myLog = Logger.getLogger(SteeringTab.class); // @jve:decl-index=0:
	
	/**
	 * This is the default constructor
	 */
	public SteeringTab() {
		super();

		properties = ProgramSettings.getProgramSettings();
		suitcase = new Suitcase();
		camera = new Camera();

		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLayout(new BorderLayout());
		this.add(getSteeringInfoPanel(), BorderLayout.NORTH);
		this.add(getCameraPanel(), BorderLayout.CENTER);
		this.add(getRemotePanel(), BorderLayout.SOUTH);

		updateInfoLables(localization.getProperty("loc-SteeringTab-welcome"), " ", " ", " ");
	}

	private void updateInfoLables(String text1, String text2, String text3,
			String text4) {
		if (infoLine1 != null) {
			infoLine1.setText(text1);
		}
		if (infoLine2 != null) {
			infoLine2.setText(text2);
		}
		if (infoLine3 != null) {
			infoLine3.setText(text3);
		}
		if (infoLine4 != null) {
			infoLine4.setText(text4);
		}
	}

	/**
	 * switchKIMode methode zum umschalten des koffers zwischen fernsteuermodus
	 * und ki
	 * 
	 * @param mode
	 *            boolscher wert der sagt ob ki ein oder aus sein soll
	 */
	private void switchKIMode() {
		getSuitcase().setRemoteMode(!getSuitcase().getRemoteMode());
		updateInfoLables(localization.getProperty("loc-SteeringTab-infoLine1")
				+ getSuitcase().getRemoteMode(), " - ", localization
				.getProperty("loc-SteeringTab-infoLine2")
				+ getSuitcase().getCommand().toString(), localization
				.getProperty("loc-SteeringTab-infoLine3")
				+ localization.getProperty("loc-SteeringTab-infoLine4"));
	}

	/**
	 * selbstsprechend
	 */
	private void goFaster() {
		getSuitcase().speedIncrease();
		updateInfoLables(localization.getProperty("loc-SteeringTab-infoLine1")
				+ getSuitcase().getRemoteMode(), " - ", localization
				.getProperty("loc-SteeringTab-infoLine2")
				+ getSuitcase().getCommand().toString(), localization
				.getProperty("loc-SteeringTab-infoLine3")
				+ localization.getProperty("loc-SteeringTab-infoLine4"));
	}

	/**
	 * selbstsprechend
	 */
	private void stop() {
		getSuitcase().emergencyStop();
		updateInfoLables(localization.getProperty("loc-SteeringTab-infoLine1")
				+ getSuitcase().getRemoteMode(), " - ", localization
				.getProperty("loc-SteeringTab-infoLine2")
				+ getSuitcase().getCommand().toString(), localization
				.getProperty("loc-SteeringTab-infoLine3")
				+ localization.getProperty("loc-SteeringTab-infoLine4"));
	}

	/**
	 * selbstsprechend
	 */
	private void goSlower() {
		getSuitcase().speedDecrease();
		updateInfoLables(localization.getProperty("loc-SteeringTab-infoLine1")
				+ getSuitcase().getRemoteMode(), " - ", localization
				.getProperty("loc-SteeringTab-infoLine2")
				+ getSuitcase().getCommand().toString(), localization
				.getProperty("loc-SteeringTab-infoLine3")
				+ localization.getProperty("loc-SteeringTab-infoLine4"));
	}

	/**
	 * selbstsprechend
	 */
	private void turnRight() {
		getSuitcase().directionRight();
		updateInfoLables(localization.getProperty("loc-SteeringTab-infoLine1")
				+ getSuitcase().getRemoteMode(), " - ", localization
				.getProperty("loc-SteeringTab-infoLine2")
				+ getSuitcase().getCommand().toString(), localization
				.getProperty("loc-SteeringTab-infoLine3")
				+ localization.getProperty("loc-SteeringTab-infoLine4"));

	}

	/**
	 * selbstsprechend
	 */
	private void turnLeft() {
		getSuitcase().directionLeft();
		updateInfoLables(localization.getProperty("loc-SteeringTab-infoLine1")
				+ getSuitcase().getRemoteMode(), " - ", localization
				.getProperty("loc-SteeringTab-infoLine2")
				+ getSuitcase().getCommand().toString(), localization
				.getProperty("loc-SteeringTab-infoLine3")
				+ localization.getProperty("loc-SteeringTab-infoLine4"));
	}

	/**
	 * selbstsprechend
	 */
	private void startSuit() {
		getSuitcase().start();
	}

	private void stopSuit() {
		getSuitcase().stirb();
	}

	private Suitcase getSuitcase() {
		if (suitcase == null) {
			suitcase = new Suitcase();
		}
		return suitcase;
	}

	/**
	 * selbstsprechend
	 */
	private void startCam() {
		camera.start();
	}

	private void stopCam() {
		camera.stirb();
	}

	private void getMockSuitcase() {
		getSuitcase().stirb();
		suitcase = new MockSuitcase();
	}

	private void editProperties() {
	}

	/**
	 * This method initializes remotePanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getRemotePanel() {
		if (remotePanel == null) {
			GridBagConstraints gridBagConstraints19 = new GridBagConstraints();
			gridBagConstraints19.gridx = 3;
			gridBagConstraints19.gridy = 0;
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.gridx = 3;
			gridBagConstraints14.gridy = 1;
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.gridx = 6;
			gridBagConstraints13.gridy = 1;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 3;
			gridBagConstraints9.gridy = 6;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 5;
			gridBagConstraints12.gridy = 4;
			jLabel3 = new JLabel();
			jLabel3.setText("   ");
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 1;
			gridBagConstraints11.gridy = 4;
			jLabel2 = new JLabel();
			jLabel2.setText("   ");
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.gridy = 1;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 3;
			gridBagConstraints10.gridy = 2;
			jLabel1 = new JLabel();
			jLabel1.setText("   ");
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.gridy = 3;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 3;
			gridBagConstraints6.gridy = 4;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 3;
			gridBagConstraints5.gridy = 5;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 4;
			gridBagConstraints4.gridy = 4;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 2;
			gridBagConstraints2.gridy = 4;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 6;
			gridBagConstraints1.gridy = 3;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 3;
			gridBagConstraints3.gridy = 3;
			remotePanel = new JPanel();
			remotePanel.setLayout(new GridBagLayout());

			remotePanel.setSize(new Dimension(320, 200));
			remotePanel.add(getFasterButton(), gridBagConstraints3);
			remotePanel.add(getRemoteButton(), gridBagConstraints1);
			remotePanel.add(getLeftButton(), gridBagConstraints2);
			remotePanel.add(getRightButton(), gridBagConstraints4);
			remotePanel.add(getSlowerButton(), gridBagConstraints5);
			remotePanel.add(getStopButton(), gridBagConstraints6);
			remotePanel.add(getSuitcaseButton(), gridBagConstraints8);
			remotePanel.add(jLabel1, gridBagConstraints10);
			remotePanel.add(getCameraButton(), gridBagConstraints7);
			remotePanel.add(jLabel2, gridBagConstraints11);
			remotePanel.add(jLabel3, gridBagConstraints12);
			remotePanel.add(getJLabel4(), gridBagConstraints9);
			remotePanel.add(getRecordButton(), gridBagConstraints13);
			remotePanel.add(getSnapButton(), gridBagConstraints14);
			remotePanel.add(jLabel5, gridBagConstraints19);
		}
		return remotePanel;
	}

	/**
	 * This method initializes fasterButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getFasterButton() {
		if (fasterButton == null) {
			fasterButton = new JButton();
			fasterButton.setText(localization
					.getProperty("loc-SteeringTab-fasterButton"));
			fasterButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					goFaster();
				}
			});
		}
		return fasterButton;
	}

	/**
	 * This method initializes remoteButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getRemoteButton() {
		if (remoteButton == null) {
			remoteButton = new JButton();
			remoteButton.setText(localization
					.getProperty("loc-SteeringTab-remoteButton"));
			remoteButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					switchKIMode();
				}
			});
		}
		return remoteButton;
	}

	/**
	 * This method initializes leftButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getLeftButton() {
		if (leftButton == null) {
			leftButton = new JButton();
			leftButton.setText(localization
					.getProperty("loc-SteeringTab-leftButton"));
			leftButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					turnLeft();
				}
			});
		}
		return leftButton;
	}

	/**
	 * This method initializes rightButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getRightButton() {
		if (rightButton == null) {
			rightButton = new JButton();
			rightButton.setText(localization
					.getProperty("loc-SteeringTab-rightButton"));
			rightButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					turnRight();
				}
			});
		}
		return rightButton;
	}

	/**
	 * This method initializes slowerButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSlowerButton() {
		if (slowerButton == null) {
			slowerButton = new JButton();
			slowerButton.setText(localization
					.getProperty("loc-SteeringTab-slowerButton"));
			slowerButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					goSlower();
				}
			});
		}
		return slowerButton;
	}

	/**
	 * This method initializes stopButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getStopButton() {
		if (stopButton == null) {
			stopButton = new JButton();
			stopButton.setText(localization
					.getProperty("loc-SteeringTab-stopButton"));
			stopButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					stopSuit();
				}
			});
		}
		return stopButton;
	}

	/**
	 * This method initializes suitcaseButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSuitcaseButton() {
		if (suitcaseButton == null) {
			suitcaseButton = new JButton();
			suitcaseButton.setText(localization
					.getProperty("loc-SteeringTab-suitcaseButton"));
			suitcaseButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							startSuit();
						}
					});
		}
		return suitcaseButton;
	}

	/**
	 * This method initializes steeringInfoPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getSteeringInfoPanel() {
		if (steeringInfoPanel == null) {
			jLabel5 = new JLabel();
			jLabel5.setText("   ");
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.gridx = 0;
			gridBagConstraints16.gridy = 0;
			GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
			gridBagConstraints15.gridx = 0;
			gridBagConstraints15.gridy = 1;
			GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
			gridBagConstraints17.gridx = 0;
			gridBagConstraints17.gridy = 2;
			GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
			gridBagConstraints18.gridx = 0;
			gridBagConstraints18.gridy = 3;
			infoLine1 = new JLabel();
			infoLine1.setText("Line 1");
			infoLine2 = new JLabel();
			infoLine2.setText("Line 2");
			infoLine3 = new JLabel();
			infoLine3.setText("Line 3");
			infoLine4 = new JLabel();
			infoLine4.setText("Line 4");
			steeringInfoPanel = new JPanel();
			steeringInfoPanel.setLayout(new GridBagLayout());
			steeringInfoPanel.setMaximumSize(new Dimension(9999, 20));
			steeringInfoPanel.add(infoLine1, gridBagConstraints16);
			steeringInfoPanel.add(infoLine2, gridBagConstraints15);
			steeringInfoPanel.add(infoLine3, gridBagConstraints17);
			steeringInfoPanel.add(infoLine4, gridBagConstraints18);
		}
		return steeringInfoPanel;
	}

	/**
	 * This method initializes imagePanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getImagePanel() {
		if (imagePanel == null) {
			imagePanel = camera.getImagePanel();
			// imagePanel.add(getCameraPanel(), null);
			// setupImagePanel(imagePanel);
			imagePanel.setPreferredSize(imagePanel.getPreferredSize());
		}
		return imagePanel;
	}

	/**
	 * This method initializes cameraPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getCameraPanel() {
		if (cameraPanel == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			cameraPanel = new JPanel();
			cameraPanel.setLayout(new GridBagLayout());
			cameraPanel.add(getImagePanel(), gridBagConstraints);
			// cameraPanel.add(jLabel, gridBagConstraints);
		}
		return cameraPanel;
	}

	/**
	 * This method initializes cameraButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCameraButton() {
		if (cameraButton == null) {
			cameraButton = new JButton();
			cameraButton.setText(localization
					.getProperty("loc-SteeringTab-cameraButton"));
		}
		return cameraButton;
	}

	/**
	 * This method initializes jLabel4
	 * 
	 * @return javax.swing.JLabel
	 */
	private JLabel getJLabel4() {
		if (jLabel4 == null) {
			jLabel4 = new JLabel();
			jLabel4.setText("   ");
		}
		return jLabel4;
	}

	/**
	 * This method initializes recordButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getRecordButton() {
		if (recordButton == null) {
			recordButton = new JButton();
			recordButton.setText(localization
					.getProperty("loc-SteeringTab-recordButton"));
		}
		return recordButton;
	}

	/**
	 * This method initializes snapButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSnapButton() {
		if (snapButton == null) {
			snapButton = new JButton();
			snapButton.setText(localization
					.getProperty("loc-SteeringTab-snapButton"));
		}
		return snapButton;
	}

}
